package homework00110;

import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.function.IntConsumer;

public class ZeroEvenOdd {
	private int n;
    private volatile int state=0;
    
    public ZeroEvenOdd(int n) {
        this.n = n;
    }
    public void zero(IntConsumer printNumber) throws InterruptedException {
        for(int i=1;i<=n;i++) {
        	synchronized (this) {
        		while(state!=0){
                	wait();
                }
                printNumber.accept(0);
                if(i%2==0){
                	state=1;
                }else{
                	state=2;
                }
                notifyAll();
			}
        }
    }
    public void even(IntConsumer printNumber) throws InterruptedException {
    	for(int i=2;i<=n;i+=2) {
    		synchronized (this) {
    			while(state!=1){
    				wait();
                }
                printNumber.accept(i);
                state=0;
                notifyAll();
			}
        }
    }
    public void odd(IntConsumer printNumber) throws InterruptedException {
    	for(int i=1;i<=n;i+=2) {
    		synchronized (this) {
    			while(state!=2){
    				wait();
                }
                printNumber.accept(i);
                state=0;
                notifyAll();
			}
        }
    }
}
